package com.mb.app.controller;

import com.mb.app.domain.TbAppInfo;
import com.mb.app.domain.TbAppChannelConfig;
import com.mb.app.service.ITbAppChannelConfigService;
import com.mb.app.service.ITbAppInfoService;
import com.mb.common.controller.BaseController;
import com.mb.common.utils.DateUtils;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import com.mb.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author zwl
 * @program
 * @description 渠道管理
 * @date 2019-03-22 16:40
 **/
@Controller
@RequestMapping("/app/appChannel")
@Slf4j
public class AppChannelController extends BaseController {

    

    @Autowired
    private ITbAppInfoService appInfoService;
    
    @Autowired
    private ITbAppChannelConfigService  appChannelConfigService;

    @GetMapping("/index")
    String index(Model model){
        // 查询应用信息
        Map<String,Object> map = new HashMap<String,Object>();
        List<TbAppInfo> appInfos = appInfoService.getAppInfoList(map);
        model.addAttribute("appInfos",appInfos);
        return "/app/appChannel/appChannel_list";
    }


    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        // 时间截取
        String createTime = (String) params.get("createTime");
        if (StringUtils.isNotEmpty(createTime)) {
            params.put("startTime", DateUtils.parse(createTime.substring(0,10),DateUtils.DATE_PATTERN));
            params.put("endTime",createTime.substring(13,23)+" 23:59:59");
        }
        Query query = new Query(params);
        List<TbAppChannelConfig> list = appChannelConfigService.queryList(query);
        int total = appChannelConfigService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    @GetMapping("/add")
    String add(Model model){
        // 查询应用信息
        Map<String,Object> map = new HashMap<String,Object>();
        List<TbAppInfo> appInfos = appInfoService.getAppInfoList(map);
        model.addAttribute("appInfos",appInfos);
        return "/app/appChannel/appChannel_add";
    }


    @ResponseBody
    @PostMapping("/save")
    public R save(TbAppChannelConfig TbAppChannelConfig, HttpServletRequest request){
        TbAppChannelConfig.setCreateUserId(getUserId());
        int result = appChannelConfigService.save(TbAppChannelConfig);
        return R.ok();
    }


    @ResponseBody
    @PostMapping("/remove")
    public R remove(@RequestParam("appChannelId") Long appChannelId, HttpServletRequest request){
        int result = appChannelConfigService.remove(appChannelId);
        return R.ok();
    }

    @GetMapping("/dtl/{appChannelId}")
    String dtl(@PathVariable("appChannelId") Long appChannelId, Model model){
        model.addAttribute("appChannelInfo",appChannelConfigService.getDtl(appChannelId));
        return "/app/appChannel/appChannel_info";
    }


    @GetMapping("/edit/{appChannelId}")
    String edit(@PathVariable("appChannelId") Long appChannelId, Model model){
        model.addAttribute("appChannelInfo",appChannelConfigService.getDtl(appChannelId));
        // 查询应用信息
        Map<String,Object> map = new HashMap<String,Object>();
        List<TbAppInfo> appInfos = appInfoService.getAppInfoList(map);
        model.addAttribute("appInfos",appInfos);
        return "/app/appChannel/appChannel_edit";
    }


    @ResponseBody
    @PostMapping("/update")
    public R update(TbAppChannelConfig TbAppChannelConfig, HttpServletRequest request){
        TbAppChannelConfig.setUpdateUserId(getUserId());
        int result = appChannelConfigService.update(TbAppChannelConfig);
        return R.ok();
    }

    /**
     * 渠道名称、渠道标识代码是否已存在
     */
    @PostMapping("/channelNameCodeExit")
    @ResponseBody
    boolean channelNameCodeExit(@RequestParam Map<String, Object> params) {
        // 存在，不通过，false
        return !appChannelConfigService.channelNameCodeExit(params);
    }

}
